package com.cat.dataStructure03;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/split-message-based-on-limit/description/
 * @create 2025/11/17 16:12
 * @since JDK17
 */

class Solution77 {
    public String[] splitMessage(String message, int limit) {
        int n = message.length();
        for (int i = 1, cap = 0, len;; i++) {
            if (i < 10) {
                len = 5;
            } else if (i < 100) {
                if (i == 10)
                    cap -= 9;
                len = 7;
            } else if (i < 1000) {
                if (i == 100)
                    cap -= 99;
                len = 9;
            } else {
                if (i == 1000)
                    cap -= 999;
                len = 11;
            }
            if (len >= limit)
                return new String[0];
            cap += limit - len;
            if (cap < n) {
                continue;
            }
            var ans = new String[i];
            for (int j = 0, k = 0; j < i; j++) {
                var tail = "<" + (j + 1) + "/" + i + ">";
                if (j == i - 1) {
                    ans[j] = message.substring(k) + tail;
                } else {
                    var m = limit - tail.length();
                    ans[j] = message.substring(k, k + m) + tail;
                    k += m;
                }
            }
            return ans;
        }
    }
}